package com.lottery.ruok.lottrey.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.CategoryAdapter;
import com.lottery.ruok.lottrey.chat.model.system.SystemMessage;
import com.lottery.ruok.lottrey.eventbusObject.PrizePushInfo;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.TimerTask;
import butterknife.BindView;
import butterknife.ButterKnife;
import count_down.CountDownManager;
import utils.Utils;

import static com.lottery.ruok.lottrey.R.id.listview;

/**
 * Created by ASUS on 2017/7/27.
 */

public class CategoryFragment extends BaseFragment {
    public String ANALYTIC_VERSION = "1.0";

    @BindView(listview)
    ListView listView;


    private CategoryAdapter categoryAdapter;

    private TimerTask task;
    private GetWholeCategoryResponse data;
    private CountDownManager.CountDownModel countDownModel;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(task != null){
            task.cancel();
            task = null;
        }
    }

    public static CategoryFragment newInstance(int index){
        CategoryFragment fragment = new CategoryFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("index",index);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.category_lottery_fragment, container, false);

        ButterKnife.bind(this, view);

        init();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        EventBus.getDefault().register(this);
    }

    public void setDatas(GetWholeCategoryResponse g){
        data = g;
        if(categoryAdapter != null){
            categoryAdapter.setData(data);
            categoryAdapter.notifyDataSetChanged();
        }else{
            categoryAdapter = new CategoryAdapter(getActivity());
            categoryAdapter.setList(data.getItems());
        }
    }

    private void init() {

        downCount();

        categoryAdapter = new CategoryAdapter(getContext());
        listView.setAdapter(categoryAdapter);

        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                setSwipState();
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    private void downCount() {
        countDownModel = new CountDownManager.CountDownModel() {
            @Override
            public void onExecute() {
                if (data == null) {
                    return;
                }
                for (GetWholeCategoryResponse.ItemsBean itemsBean : data.getItems()) {
                    itemsBean.pasttime++;
                }

                if (categoryAdapter != null) {
                    categoryAdapter.refreshTime();
//                    categoryAdapter.notifyDataSetChanged();
                }
            }
        };
        CountDownManager.asInstance().register(countDownModel);
    }

    @Override
    public void onDetach() {
        CountDownManager.asInstance().unRegister(countDownModel);
        EventBus.getDefault().unregister(this);
        super.onDetach();
    }

    public void setSwipState(){
        if (listView != null && listView.getChildAt(0) != null) {
            ((BuyLotteryMainFragment)getParentFragment()).swipeRefreshLayout.setEnabled(listView.getFirstVisiblePosition() == 0 && listView.getChildAt(0).getTop() == 0);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PrizePushInfo event) {
        if (event.getCode() != null) {
            if(data == null){
                return;
            }
            List<GetWholeCategoryResponse.ItemsBean> items = data.getItems();
            for(GetWholeCategoryResponse.ItemsBean item : items){
                if(item.getCode().equals(event.getCode())){
                    item.setPrize_num(event.getPrizeNum());
                    item.setNo(String.valueOf(Long.parseLong(event.getIssueNo())+ 1));
                    item.setTime(item.getInterval());
                    categoryAdapter.notifyDataSetChanged();
                }
            }
        }
    }
}